package com.honor.games.wg.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.badlogic.gdx.math.Vector2;
import com.honor.games.wg.config.ApplicationConfiguration;
import com.honor.games.wg.entities.WGCoordinates;

import static com.google.common.base.Preconditions.*;

@Service
public class WGMathServices {

	@Autowired
	private ApplicationConfiguration config;

	private Vector2 realCoordinates = new Vector2();

	public Vector2 calculateRealCoordinates(final int x, final int y) {

		checkArgument(x >= 0 && y >= 0 && x < config.getBoardTilesWidthNumber() && y < config.getBoardTilesHeightNumber(),
				"x and y must be between the board bounds. x, y >= 0, x <= %s, y <= %s", config.getBoardTilesWidthNumber() - 1,
				config.getBoardTilesHeightNumber() - 1);

		realCoordinates.x = initialPosition.x + (config.getBoardTilesSizeWidth() * x);
		realCoordinates.y = initialPosition.y + (config.getBoardTilesSizeHeight() * y);
		return realCoordinates.cpy();
	}

	// TODO : clarify between tile coordinates and system coordinates
	public WGCoordinates calculateTileCoordinates(final float x, final float y, boolean fromTileSystem) {
		
		int locationX = -1;
		int locationY = -1;
		
		if(!fromTileSystem) {
			if(x - initialPosition.x > 0) {
				locationX = locationX / config.getBoardTilesSizeWidth();	
			}
			
			if(y - initialPosition.y > 0) {
				locationY = locationY / config.getBoardTilesSizeHeight();	
			}
		} else {
			locationX = (int) (x / config.getBoardTilesSizeWidth());
			locationY = (int) (y / config.getBoardTilesSizeHeight());
		}
		

		WGCoordinates coordinates = new WGCoordinates(locationX, locationY);		
		return coordinates;
	}
	
	private Vector2 initialPosition = new Vector2();

	public void setInitialPosition(Vector2 initialPosition) {
		this.initialPosition = initialPosition.cpy();
	}
}
